**************************************************************************************************************************************
*                  REGRESSIONS WITH TIME-VARYING COEFFICIENTS BY FISCAL YEAR WITH PATIENT FES, TRADITIONAL MEDICARE ONLY             *
* Last updated: 01/02/2023 																											 *
**************************************************************************************************************************************
log using "${SIDCodePath}/sidsedd_regressions_trad_fyear.log", replace

*--------------------------------------------------
* Difference in RD by fiscal year
*--------------------------------------------------
use "${SIDDataPath}/sidsedd_merged_20102016.dta", clear
gen 	year_dqtr2 		  = year_dqtr * 100 
gen 	fyear = year
replace fyear = year + 1 if dqtr == 0.75


foreach var of varlist out  nxtvi_30d obs inpatient {
	forval t = 3/3 {
		preserve
		
			// Make estimates 
				di "q_med_`var'_`t'h_diRD"
				keep if hourstilMN`t' == 1
				keep if trad_medicare == 1

				reghdfe `var' after_MN##ib2013.fyear, ///
					absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
					vce(cluster edhour_2 year_dqtr)

				parmest, ///
					saving("${SIDOutputPath}/Coefficients/reg_trad_patientFEs/fy_med_`var'_`t'h_diRD_controls.dta", replace)   
		
			
			
		restore
	}
} 

// Plot coefficients
local vars "out  nxtvi_30d obs inpatient"
foreach var of local vars{
	forval t = 3/3{
	
		use  	 "${SIDOutputPath}/Coefficients/reg_trad_patientFEs/fy_med_`var'_`t'h_diRD_controls.dta", clear
		keep     if substr(parm, 1, 10) == "1.after_MN" | substr(parm, 1, 11) == "1o.after_MN"
		gen      fyear = substr(parm, 12, 4) if substr(parm, 1, 10) == "1.after_MN"
		replace  fyear = substr(parm, 13, 4) if substr(parm, 1, 11) == "1o.after_MN"
		destring fyear, replace
		replace estimate = 0 if fyear == 2013

		eclplot  estimate min95 max95 fyear, ///
		 		 xline(2013, lcolor(black) lpattern(dash)) yline(0) graphregion(color(white)) ///
		 		 xtitle("Year", size(large)) xlabel(2010(1)2015, labsize(large) angle(60)) ylabel(, labsize(large)) ///
		 		 ytitle("")

		graph export "${SIDOutputPath}/reg_trad_patientFEs/fy_med_`var'_`t'h_diRD_controls.eps", replace  

		twoway ( rcap min95 max95 fyear ) ( scatter estimate fyear, m(o) c(l) lp(line) ), ///
				xline(2013, lcolor(black) lpattern(dash)) yline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) ///
		 		xtitle("Year", size(large)) xlabel(2010(1)2015, labsize(large) angle(60)) ylabel(, labsize(large)) ///
		 		ytitle("") legend(off)
		 graph export "${SIDOutputPath}/reg_trad_patientFEs/fyconnected_med_`var'_`t'h_diRD_controls.eps", replace  
	}
}
	

log close

